American Mineralogist: September 2018 Deposit AM-18-96546
HUGHES ET AL.: HIGH-RES EPMA OF FE OXIDATION STATE IN SILICA GLASSES

# Calculate exponential fits 
# Data needs to be a csv file and columns must be in order time, s.d., Fe Lb/La, s.d. although can have data in columns before and after this (use k <- to say which column number refers to the time column of the Fe Lb/La data). 
 
k <- 9 # specify which column number the time data for Fe Lb/La is 
n <- 24 # specify number of timesteps 
t <- 150 # total time for single analysis 
 
# The following section applies to data which converge when Iinf is NOT set to the final value of the time series. 
# Import data: Paste location of data inbetween " " 
setwd("") 
list_tot1 <- list.files(path = ".", pattern="*.dat") 
 
for (i in 1:1) { 
  data <- read.table(file = list_tot1[i], header = TRUE, fill = TRUE) 
  data_num <- as.matrix(sapply(data, as.numeric)) 
   
  titles <- cbind("Sample", "I0", "I0 s.d.", "Iinf", "Iinf s.d.", "I'0", "I'0 s.d." ) 
   
  fit <- nls(formula = data_num[,k+2] ~ (a-b)*exp((c*data_num[,k])/(a-b))+b, start = list(a=data_num[1,k+2],b=data_num[n,k+2],c=((data_num[n,k+2]-data_num[1,k+2])/t))) 
  coeff <- as.numeric(coef(fit)) 
  coeff_sd <- as.numeric(coef(summary(fit))[, "Std. Error"]) 
  sumFeLba <- cbind(coeff[1],coeff_sd[1], coeff[2], coeff_sd[2], coeff[3], coeff_sd[3]) 
   
   
  # combine 
  fit_sum <- cbind(list_tot1[i], sumFeLba) 
  colnames(fit_sum) <- titles} 
 
 
for (i in 2:length(list_tot2)) { 
   
  data <- read.table(file = list_tot1[i], header = TRUE, fill = TRUE) 
  data_num <- as.matrix(sapply(data, as.numeric)) 
  print(list_tot1[i]) 
   
  fit <- nls(formula = data_num[,k+2] ~ (a-b)*exp((c*data_num[,k])/(a-b))+b, start = list(a=data_num[1,k+2],b=data_num[n,k+2],c=((data_num[n,k+2]-data_num[1,k+2])/t))) 
  coeff <- as.numeric(coef(fit)) 
  coeff_sd <- as.numeric(coef(summary(fit))[, "Std. Error"]) 
  sumFeLba <- cbind(coeff[1],coeff_sd[1], coeff[2], coeff_sd[2], coeff[3], coeff_sd[3]) 
   
  # combine 
  fit_sumx <- cbind(list_tot1[i], sumFeLba) 
  fit_sum <- rbind(fit_sum, fit_sumx)} 
 
# The following section applies to data which converge when Iinf is set to the final value of the time series. 
# Import data: Paste location of data inbetween " " 
setwd("") 
list_tot2 <- list.files(path = ".", pattern="*.dat") 
 
for (i in 1:1) { 
  data <- read.table(file = list_tot2[i], header = TRUE, fill = TRUE) 
  data_num <- as.matrix(sapply(data, as.numeric)) 
   
  titles <- cbind("Sample", "I0", "I0 s.d.", "Iinf", "Iinf s.d.", "I'0", "I'0 s.d." ) 
   
  fit <- nls(formula = data_num[,k+2] ~ (a-data_num[n,k+2])*exp((c*data_num[,k])/(a-data_num[n,k+2]))+data_num[n,k+2], start = list(a=data_num[1,k+2],c=((data_num[n,k+2]-data_num[1,k+2])/t))) 
  coeff <- as.numeric(coef(fit)) 
  coeff_sd <- as.numeric(coef(summary(fit))[, "Std. Error"]) 
  sumFeLba <- cbind(list_tot2[i],coeff[1],coeff_sd[1], data_num[n,k+2], data_num[n,k+3], coeff[2], coeff_sd[2]) 
   
   
  } 
 
 
for (i in 2:length(list_tot2)) { 
   
  data <- read.table(file = list_tot2[i], header = TRUE, fill = TRUE) 
  data_num <- as.matrix(sapply(data, as.numeric)) 
  print(list_tot2[i]) 
   
  fit <- nls(formula = data_num[,k+2] ~ (a-data_num[n,k+2])*exp((c*data_num[,k])/(a-data_num[n,k+2]))+data_num[n,k+2], start = list(a=data_num[1,k+2],c=((data_num[n,k+2]-data_num[1,k+2])/t))) 
  coeff <- as.numeric(coef(fit)) 
  coeff_sd <- as.numeric(coef(summary(fit))[, "Std. Error"]) 
  sumFeLbax <- cbind(list_tot2[i],coeff[1],coeff_sd[1], data_num[n,k+2], data_num[n,k+3], coeff[2], coeff_sd[2]) 
   
  # combine 
  sumFeLba <- rbind(sumFeLba, sumFeLbax)} 
 
   
# Combines data from variable Iinf and constant Iinf 
  summary <- rbind(fit_sum,sumFeLba) 
 
# The following section applies to data which is stable. 
# Import data: Paste location of data inbetween " " 
setwd("") 
list_tot3 <- list.files(path = ".", pattern="*.dat") 
 
for (i in 1:1) { 
  data <- read.table(file = list_tot3[i], header = TRUE, fill = TRUE) 
  data_num <- as.matrix(sapply(data, as.numeric)) 
   
  titles <- cbind("Sample", "I0", "I0 s.d.", "Iinf", "Iinf s.d.", "I'0", "I'0 s.d." ) 
   
  average <- mean(data_num[,k+2]) 
  sd <- sd(data_num[,k+2]) 
  sumFeLba2 <- cbind(list_tot3[i], average, sd, 0, 0, 0, 0)} 
 
 
for (i in 2:length(list_tot3)) { 
   
  data <- read.table(file = list_tot3[i], header = TRUE, fill = TRUE) 
  data_num <- as.matrix(sapply(data, as.numeric)) 
  print(list_tot3[i]) 
   
  average <- mean(data_num[,k+2]) 
  sd <- sd(data_num[,k+2]) 
  sumFeLbax <- cbind(list_tot3[i], average, sd, 0, 0, 0, 0) 
   
  # combine 
  sumFeLba2 <- rbind(sumFeLba2, sumFeLbax)} 
 
 
# Combines data from variable Iinf/constant Iinf with stable data 
summary2 <- rbind(summary,sumFeLba2) 
 
# Saves summary table - choose where to save it 
setwd("") 
write.table(summary2, file = "summary.dat",row.names = FALSE) 
 

